package com.unhz.taigu.service.impl;

import com.unhz.generator.dao.ReportMapper;
import com.unhz.generator.pojo.Report;
import com.unhz.taigu.service.ReportImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author: 沈佳庆
 * @Description: 质检报告图片
 * @Date: Created in 上午 11:15 2018/2/6 0006
 * @Modified By: V1.0.0
 */
@Service
public class ReportImageServiceImpl implements ReportImageService{
    static final int EFFECTIVE = 1;
    static final int BOUND = 1000;
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public void insertReportImage(Integer insoleId, MultipartFile[] report, HttpServletRequest request) {
        //保存中底图片信息
        File targetFile=null;
        String msg="";//返回存储路径
        //int code=1;
        //List imgList=new ArrayList();
        Report reports = null;
        if (report!=null && report.length>0) {
            for (int i = 0; i < report.length; i++) {
                String fileName=report[i].getOriginalFilename();//获取文件名加后缀
                if(fileName!=null&&fileName!=""){
                    String returnUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() +"/upload/imgs/";//存储路径
                    String path = request.getSession().getServletContext().getRealPath("upload/imgs"); //文件存储位置
                    String fileF = fileName.substring(fileName.lastIndexOf("."), fileName.length());//文件后缀
                    fileName=new Date().getTime()+"_"+new Random().nextInt(BOUND)+fileF;//新的文件名
                    //先判断文件是否存在
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                    String fileAdd = sdf.format(new Date());
//                    String fileAdd = DateUtil.format(new Date(),"yyyyMMdd");
                    File file1 =new File(path+"/"+fileAdd);
                    //如果文件夹不存在则创建
                    if(!file1.exists()  && !file1.isDirectory()){
                        file1.mkdirs();
                    }
                    targetFile = new File(file1, fileName);
                    try {
                        report[i].transferTo(targetFile);
                        msg=returnUrl+fileAdd+"/"+fileName;
                        reports = new Report();
                        reports.setInsoleId(insoleId);
                        reports.setReportUrl(msg);
                        reports.setCreateTime(new Date());
                        reports.setValid(EFFECTIVE);
                        reportMapper.insertSelective(reports);
//                        imgList.add(msg);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
//        return JSON.toJSONString(imgList);
    }

    @Override
    public int insertSelective(Report pojo) {
        return reportMapper.insertSelective(pojo);
    }

    public List<Report> selectReportImageList(Integer insoleId) {
        return reportMapper.selectReportImageList(insoleId);

    }

    @Override
    public String[] selectImageById(Integer id) {
        return reportMapper.selectImageById(id);
    }

    @Override
    public void deleteReportImage(Integer insoleId) {
        reportMapper.deleteReportImage(insoleId);
    }
}
